package com.leetcode.offer.chapter4;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @author Dennis Li
 * @date 2020/7/13 18:48
 */
public class MinStack_30 {

    Deque<Integer> stack;
    Deque<Integer> minStack;

    public MinStack_30() {
        stack = new LinkedList<>();
        minStack = new LinkedList<>();
    }

    public void push(int x) {
        if (minStack.isEmpty() || minStack.peekLast() >= x) {
            minStack.push(x);
        }
        stack.push(x);
    }

    public void pop() {
        if(stack.isEmpty()) return;
        if (stack.pop().equals(minStack.peekLast()))
            minStack.pop();
    }

    public int top() {
        return stack.peekLast();
    }

    public int min() {
        return minStack.peekLast();
    }

}
